<?php
/*
* 公共部分
* by JJ
× time 14.02.06
*/
class pub{
	
	private $data;
	private $right;
	private $channel="liuxue";
	
	private static $_instance;
	public static function get(){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self;
		}
		return self::$_instance;
	}
	
	//系统缓存	
	private function sys(){
		import('@.Class.mcache');
		$MCache = new MCache();
		$MCache->GetCache("home/nav");		
		$MCache->GetCache("home/newsname");
		$MCache->GetCache("home/catename2");
		global $NavCache,$NewsNameCache,$CateName2Cache;
		
		//News ID 对应 URL
		$this->data['nn'] = $NewsNameCache;
		$this->data['cn'] = $CateName2Cache;
	
		//底部
		$res = M("Advertise")->where("adcode='footer'")->field("adcontent")->find();
		$this->data['footer'] = $res['adcontent'];
		
		//幻灯片
		$this->data['ad'] = MCache::get('Ad/2');
		
		//广告图
		$this->data['adv'] = MCache::get('Advertise/1');

		//首页幻灯
		$this->data['ad_index'] = MCache::get('Ad/4');
		
		//导航
		$NavCache[7]['child'] = M("News_cate")->where('pid=23')->select();//申请攻略
		$NavCache[8]['child'] = M("News_cate")->where('pid=28')->select();//留学指南
		$this->data['nav'] = $NavCache;
		
		//设置
		$SystemCache = MCache::get('channel/liuxue');
		$SystemCache = unserialize($SystemCache['sys']);
		
		$title = $SystemCache['site_name'];
		$SystemCache['title'] = $title;
		$this->data['sys'] = $SystemCache;
		
		//导航(全局公共)
		$PublicNav = MCache::get('Nav/index');
		//unset($PublicNav[0]);//干掉首页
		$this->data['PublicNav'] = $PublicNav;
		
		$this->nav();
		
		//底部导航(全局公共)
		foreach($PublicNav as $v){
			if(strpos($v['nav_url'],$this->channel)){
				continue;
			}
			$PublicFooterNav[] = $v;	
		}
		$this->data['PublicFooterNav'] = $PublicFooterNav;
		
	}
	
	//导航
	private function nav(){
		$NavCache = MCache::get('Nav/liuxue');
		$url = $_SERVER['REDIRECT_URL'];//当前URL
		//$array[] = $index;
		$array = array();
		$i=0;
		foreach($NavCache as &$v){
			if($url==$v['nav_url']){
				$v['current'] = 1;	
				$i=1;
			}
			$array[] = $v;
		}
		if($i==0){
			$array[0]['current'] = 1;
		}
		$this->data['nav'] = $array;
	}
	
	//首页留学中
	private function index_lxz(){
		$res = M("Advertise")->where("adcode='index_lxz'")->field("adcontent")->find();
		$this->data['lxz'] = $res['adcontent'];
	}
	
	//首页留学中
	private function index_lxh(){
		$res = M("Advertise")->where("adcode='index_lxh'")->field("adcontent")->find();
		$this->data['lxh'] = $res['adcontent'];
	}
	
	//首页精彩推荐	
	private function index_wonderful(){
		$res = M("Advertise")->where("adcode='index_wonderful'")->field("adcontent")->find();
		$this->data['wonderful'] = $res['adcontent'];
	}
	
	//内页右侧推荐
	private function right_tj(){
		$res = M("Advertise")->where("adcode='right_tj'")->field("adcontent")->find();
		$this->right['righttj'] = $res['adcontent'];
	}
	
	//合作伙伴
	private function partner(){
		$this->data['Partner'] = M("Partner")->select();
	}
	
	//友情链接
	private function links(){
		$Links = MCache::get('Links/1');
		$this->data['Links'] = $Links;
	}
	
	//底部文章
	private function footernew(){
		$footerCate = M("News_cate")->where('isfooter=1')->select();
		$News = M("News");
		foreach($footerCate as $k=>$v){
			$footerCate[$k]['child'] = $News->where("cid='{$v['cid']}'")->select();
		}
		$this->data['footerCate'] = $footerCate;		
	}

	//留学移民推荐
	public function rightLXYMTJ(){
		$id = '36';
		$this->right['lxymtj'] = M("News")->where("cid =".$id)->limit(3)->order('sorts desc,id desc')->select();		
	}

	//其他移民推荐
	public function rightQTYMTJ(){
		$id = '37';
		$this->right['qtymtj'] = M("News")->where("cid =".$id)->limit(3)->order('sorts desc,id desc')->select();		
	}
		
	//申请攻略
	public function rightSQGL($ncid){
		$id = '23';
		$ncidsql = $ncid > 0 ? " and ncid=".$ncid : '';
		$CatChild = M("News_cate")->where('pid='.$id)->select();
		foreach($CatChild as $k=>&$v){
			$CatChild[$k]['child'] = M("News")->where("cid =".$v['cid'].$ncidsql)->limit(4)->order('sorts desc,id desc')->select();	
		}
		$this->right['sqgl'] = $CatChild;
	}
	
	//专业排名
	public function rightZYPM($ncid){
		$id = '35';
		$ncidsql = $ncid > 0 ? " and ncid=".$ncid : '';
		$this->right['zypm'] = M("News")->where("cid =".$id.$ncidsql)->limit(4)->order('sorts desc,id desc')->select();		
	}
	
	//成功案例
	public function rightCGAL($ncid){
		$id = '38';
		$ncidsql = $ncid > 0 ? " and ncid=".$ncid : '';
		$this->right['cgal'] = M("News")->where("cid =".$id.$ncidsql)->limit(4)->order('sorts desc,id desc')->select();		
	}
	
	//加拿大留学生活指南
	public function rightLXZN($ncid){
		$id = '28';
		$ncidsql = $ncid > 0 ? " and ncid=".$ncid : '';
		$CatChild = M("News_cate")->where('pid='.$id)->select();
		foreach($CatChild as $k=>&$v){
			$CatChild[$k]['child'] = M("News")->where("cid =".$v['cid'].$ncidsql)->limit(4)->order('sorts desc,id desc')->select();	
		}
		$this->right['lxzn'] = $CatChild;	
	}
	
	/******************************************/
	
	public function settitle($title){
		return $title.$this->data['sys']['site_meta_title'];	
	}
	
	public function setother($keywords,$description){
		if(!empty($keywords)){
			$this->data['sys']['site_meta_keywords'] = $keywords;
		}
		if(!empty($description)){
			$this->data['sys']['site_meta_description'] = $description;
		}
		return $this->data;
	}
	
	/*
	*	自定义设置其他属性
	*	p 地图 c 分类
	*/
	public function DiySetData($txt,$p,$c){
		$txt=str_replace("{p}",$p,$txt);
		$txt=str_replace("{c}",$c,$txt); 	
		return $txt;
	}
	
	/*
	*	提取100个字作描述
	*/
	public function getNumDesc($txt){
		Load('extend');
		$txt = str_replace('&nbsp;','',strip_tags($txt));
		$txt = preg_replace("/[\s]{2,}/","",$txt); 
		$txt = str_replace("\n","",$txt);
		$txt = msubstr($txt,0,100,'utf-8',false);
		return $txt;
	}
	
	public function setpassage($cid,$cname,$name){
		
		if($cid==23 || $cid==28){
			$url = U('nlists2/'.$cid);
		}elseif($cid==38){
			$url = U('success/index'); 
		}else{
			$url = U('nlists/'.$cid);	
		}	
		$array[0] = '<a href="/">加拿大留学首页</a>';
		$array[1] = '<a href="'.$url.'">'.$cname.'</a>';
		$array[2] = '<a href="'.__APP__.'">'.$name.'</a>';
		$p = '<span class="ml5 mr5">></span>';
		$res = implode($p,$array);
		return $res;
	}
	
	//公共部分
	public function run(){
		$this->sys();
		$this->partner();
		$this->links();
		$this->footernew();
		return $this->data;	
	}
	
	//首页
	public function index(){
		$this->index_lxz();//留学中
		$this->index_lxh();
		$this->index_wonderful();//精彩推荐	
	}
	
	//右侧
	public function right($cid){
		$this->right_tj();
		$this->rightLXYMTJ();//留学移民推荐
		$this->rightQTYMTJ();//其他移民推荐
		$this->rightCGAL($cid);//成功案例
		$this->rightSQGL($cid);//申请攻略
		$this->rightZYPM($cid);//专业排名
		$this->rightLXZN($cid);//加拿大留学生活指南
		return $this->right;	
	}
		
}